package com.lenovo.translatetext.utils;

import android.util.Log;
import com.google.common.base.Optional;
import com.lenovo.translatetext.entity.DetectResult;
import com.lenovo.translatetext.languageDetection.detectByApi.DetectLanguage;
import com.lenovo.translatetext.languageDetection.detectByApi.Result;
import com.lenovo.translatetext.languageDetection.detectByApi.errors.APIError;
import com.optimaize.langdetect.LanguageDetector;
import com.optimaize.langdetect.i18n.LdLocale;
import com.optimaize.langdetect.text.TextObject;
import com.optimaize.langdetect.text.TextObjectFactory;

import java.io.IOException;
import java.util.List;

import static com.lenovo.translatetext.utils.FileUtils.getLanguageDetector;
import static com.lenovo.translatetext.utils.FileUtils.getTextObjectFactory;

/**
 * @ProjectName: Translation
 * @Package: com.lenovo.translatetext.utils
 * @ClassName: DetectUtils
 * @Description: DetectUtils类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/4/29 16:35
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/4/29 16:35
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DetectUtils {
    private static final String TAG = DetectUtils.class.getSimpleName();

    /**
     * 通过网络请求识别国家码
     * @param text
     * @return
     */
    public static DetectResult languageDetectByApi(String text,String compareCode) {
        if(compareCode.equals("zh-TW")){
            compareCode="zh-Hant";
        }
        DetectResult detectResult=new DetectResult();
        List<Result> results = null;
        try {
            results = DetectLanguage.detect(text);
            Log.d(TAG, "languageDetectByApi: Results"+results.size());
        } catch (APIError apiError) {
            apiError.printStackTrace();
        }
        if(results!=null&&results.size()>0){
            for(Result result:results){
                detectResult.setCode(detectResult.getCode()+"/"+result.language);
            }
            Result result = results.get(0);
            Log.d(TAG, "languageDetectByApi: Language: " + result.language);
            Log.d(TAG, "languageDetectByApi: Is reliable: " + result.isReliable);
            Log.d(TAG, "languageDetectByApi: Confidence: " + result.confidence);
            if(result!=null){
                //detectResult.setCode(result.language);
                if(compareCode.equals(result.language.trim())){
                    detectResult.setPass("Pass");
                }else{
                    detectResult.setPass("Fail");
                }
                detectResult.setReliable(result.isReliable);
                detectResult.setConfidence(result.confidence);
            }
            return detectResult;
        }
        return detectResult;
    }

    /**
     * 国家码识别（本地识别库）
     * Github地址：https://github.com/optimaize/language-detector
     *
     * @param text 字符
     * @return 国家码
     */
    public static DetectResult languageDetectByJar(String text,String compareCode) throws IOException {
        //解析工具
        LanguageDetector languageDetector=getLanguageDetector();
        TextObjectFactory textObjectFactory=getTextObjectFactory();
        DetectResult result =new DetectResult();
        //query:
        TextObject textObject = textObjectFactory.forText(text);
        Log.d(TAG, "languageDetectByJar: textAfterFilter--->"+textObject);
        Optional<LdLocale> lang = languageDetector.detect(textObject);
        Log.d(TAG, "languageDetectByJar: Optional--->"+lang.toString());
        if(lang.isPresent()){
            Log.d(TAG, "languageDetectByJar: "+lang.get().toString());
            result.setCode(lang.get().getLanguage());
        }
        return result;
    }

    /**
     * 国家码识别（本地识别库）
     * Github地址：https://github.com/shuyo/language-detection
     * @param text
     * @return
     */
    public static DetectResult languageDetectByJar2(String text){

        return null;

    }
}
